package morfo.jgen;

import java.util.Vector;

/**
 * 
 * @author Petri Uusitalo 2007 
 *
 */
public class StemRewriteRule {

	public static final int BASE = 1;
	public static final int HARD = 2;
	public static final int SOFT = 3;

	private final Vector<String> sourceEndings = new Vector<String>();
	private final Vector<String> destinationEndings = new Vector<String>();
	private final Vector<Integer> formIdentifiers = new Vector<Integer>();

	private String stemClass = null;

	public StemRewriteRule(final String stemClassName) {
		this.stemClass = stemClassName;
	}

	public void print() {
		System.out.println("   stemClass - " + stemClass);
		for (int a = 0; a < sourceEndings.size(); a++) {
			System.out.println("   " + a + ".  " + sourceEndings.get(a) + " - "
					+ destinationEndings.get(a) + " - "
					+ formIdentifiers.get(a));
		}
	}

	@Override
	public String toString() {

		final StringBuffer buffer = new StringBuffer();

		buffer.append(stemClass);
		buffer.append("\t");

		for (int a = 0; a < sourceEndings.size(); a++) {
			buffer.append(sourceEndings.get(a));
			buffer.append("->");
			buffer.append(destinationEndings.get(a));
			buffer.append(" (");
			buffer.append(formIdentifiers.get(a));
			buffer.append("),");
		}
		return buffer.toString();
	}

	public String getStemClassName() {
		return stemClass;
	}

	public void addRule(final int formIdentifier, final String sourceEnding,
			final String destinationEnding) {
		formIdentifiers.add(formIdentifier);
		sourceEndings.add(sourceEnding);
		destinationEndings.add(destinationEnding);
	}

	public boolean accept(final Word word) {

		for (int a = 0; a < sourceEndings.size(); a++) {
			final String sourceEnding = sourceEndings.get(a);
			final int formIdentifier = formIdentifiers.get(a);
			switch (formIdentifier) {
			case BASE:
				if (word.getBaseForm().endsWith(sourceEnding) == true)
					return true;
				break;
			case SOFT:
				if (word.getSoftForm().endsWith(sourceEnding) == true)
					return true;
				break;
			case HARD:
				if (word.getHardForm().endsWith(sourceEnding) == true)
					return true;
				break;
			}
		}
		return false;
	}

	public String getStem(final Word word) {

		for (int a = 0; a < sourceEndings.size(); a++) {
			String sourceEnding = sourceEndings.get(a);
			String destinationEnding = destinationEndings.get(a);
			final int formIdentifier = formIdentifiers.get(a);
			
			if(sourceEnding.length() == 0) {
				sourceEnding = 
					word.getBaseForm().substring(word.getBaseForm().length()-1, word.getBaseForm().length());
				destinationEnding = sourceEnding + destinationEnding;
			}
			
			switch (formIdentifier) {
			case BASE:
				if (word.getBaseForm().endsWith(sourceEnding) == true) {
					final String stem = word.getBaseForm().substring(0,
							word.getBaseForm().lastIndexOf(sourceEnding));
					return stem + destinationEnding;
				}
			case SOFT:
				if (word.getSoftForm().endsWith(sourceEnding) == true) {
					final String stem = word.getSoftForm().substring(0,
							word.getSoftForm().lastIndexOf(sourceEnding));
					return stem + destinationEnding;
				}
			case HARD:
				if (word.getHardForm().endsWith(sourceEnding) == true) {
					final String stem = word.getHardForm().substring(0,
							word.getHardForm().lastIndexOf(sourceEnding));
					return stem + destinationEnding;
				}
			}
		}
		return null;
	}
}
